home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / plain C OS8 / Gadgets / Buttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  9.5 KB  |  433 lines  |  [TEXT/CWIE]

  1. /* Buttons.c */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15. #include <stdlib.h>
  16.  
  17. #include "Globals.h"
  18. #include "ResourceDefs.h"
  19. #include "DoScrap.h"
  20. #include "Miscellany.h"
  21. #include "Scrolling.h"
  22. #include "WindowAids.h"
  23. #include "ControlUtils.h"
  24. #include "Dispatcher.h"
  25. #include "DDocData.h"
  26. #include "GadgetsEngine.h"
  27. #include "GadgetsDoc.h"
  28. #include "Buttons.h"
  29.  
  30.  
  31. static    GadgetsEngine*      GetEngine    (Buttons*    self);
  32.  
  33. static    void    DoUndo        (Buttons*    self);
  34. static    void    DoCut        (Buttons*    self);
  35. static    void    DoCopy        (Buttons*    self);
  36. static    void    DoPaste        (Buttons*    self);
  37. static    void    DoClear        (Buttons*    self);
  38. static    void    DoSelectAll        (Buttons*    self);
  39. static    void    DoShowClipboard    (Buttons*    self);
  40.  
  41.  
  42. //----------
  43. Buttons*        NewButtons ()
  44. {
  45.     Buttons*        window;
  46.  
  47.     window = (Buttons*)malloc (sizeof (Buttons));
  48.     Buttons_Init (window);
  49.     SetClassID (window, classButtons);
  50.  
  51.     return window;
  52. }
  53.  
  54. //----------
  55. void    DeleteButtons (
  56.     Buttons*        window)
  57. {
  58.     Buttons_Free (window);
  59.     free (window);
  60. }
  61.  
  62. //----------
  63. void    Buttons_Create (
  64.     AMDoc*            inDoc,
  65.     DDocData*        inData)
  66. {
  67.     Buttons*        winObj = NewButtons ();
  68.  
  69.     if (winObj != nil) {
  70.         Buttons_Open (winObj, inDoc, inData);
  71.     }
  72. }
  73.  
  74. //----------
  75. void    Buttons_Init (
  76.     Buttons*        self)
  77. {
  78. }
  79.  
  80. //----------
  81. void    Buttons_Free (
  82.     Buttons*        self)
  83. {
  84. }
  85.  
  86. //----------
  87. GadgetsEngine*    GetEngine (
  88.     Buttons*        self)
  89. {
  90.     return (GadgetsEngine*) self->super.mDoc->mEngine;
  91. }
  92.  
  93. /*----------*/
  94. void    Buttons_Open (
  95.     Buttons*        self,
  96.     AMDoc*            inDoc,
  97.     DDocData*        inData)
  98. {
  99.     WindowPtr        window;
  100.     Handle            wftb;
  101.  
  102.     self->super.mDoc = inDoc;
  103.     self->mData = inData;
  104.     AddResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  105.  
  106.     window = GetWindow (WIND_Buttons);
  107.     if (AMEngine_GetFilename (self->super.mDoc->mEngine) [0] != 0) {
  108.         SetWTitle (window, AMEngine_GetFilename (self->super.mDoc->mEngine));
  109.     }
  110.     self->super.mWindow = window;
  111.     ((GadgetsDoc*)self->super.mDoc)->mButtonsPtr = window;
  112.  
  113.     SetWindowKind (window, 'AM');
  114.     SetWRefCon (window, (long) self);
  115.     SetPort (window);
  116.     SetInfo (window);
  117.  
  118.     wftb = GetResource ('Wftb', WIND_Buttons);
  119.  
  120.     CreateRootControl (window, &self->super.mRootControl);
  121.  
  122.     self->super.vScroll = nil;
  123.     self->super.hScroll = nil;
  124.  
  125.  
  126.     self->mStandardHandle = GetNewControl (CNTL_Standard, window);
  127.     SetWindowItemFont (self->mStandardHandle, wftb, 1);
  128.     SetDefaultState (self->mStandardHandle, true);
  129.  
  130.     self->mRightIconHandle = GetNewControl (CNTL_RightIcon, window);
  131.     SetWindowItemFont (self->mRightIconHandle, wftb, 2);
  132.  
  133.     self->mBevelHandle = GetNewControl (CNTL_Bevel, window);
  134.     SetWindowItemFont (self->mBevelHandle, wftb, 3);
  135.     SetBevelButtonTextPlacement (self->mBevelHandle, kControlBevelButtonPlaceToRightOfGraphic);
  136.     SetBevelButtonTextAlignment (self->mBevelHandle, kControlBevelButtonAlignTextFlushLeft, 0);
  137.     SetBevelButtonGraphicAlignment (self->mBevelHandle, kControlBevelButtonAlignLeft, 0, 0);
  138.  
  139.     self->mXxHandle = GetNewControl (CNTL_Xx, window);
  140.     SetWindowItemFont (self->mXxHandle, wftb, 4);
  141.     SetControlValue (self->mXxHandle, GetTriangle (self->mData));
  142.  
  143.     self->mCapTriangleLabel = GetNewControl (CNTL_CapTriangle, window);
  144.     SetWindowItemFont (self->mCapTriangleLabel, wftb, 5);
  145.     SetControlFromTEXT (self->mCapTriangleLabel, TEXT_CapTriangle);
  146.  
  147.     self->mLightHandle = GetNewControl (CNTL_Light, window);
  148.     SetWindowItemFont (self->mLightHandle, wftb, 6);
  149.  
  150.     self->mLeftRightHandle = GetNewControl (CNTL_LeftRight, window);
  151.     SetWindowItemFont (self->mLeftRightHandle, wftb, 7);
  152.     SetControlValue (self->mLeftRightHandle, GetLeftRight (self->mData));
  153.  
  154.     self->mRadiosBoxHandle = GetNewControl (CNTL_RadiosBox, window);
  155.     SetWindowItemFont (self->mRadiosBoxHandle, wftb, 8);
  156.     self->mRadiosGroupHandle = GetNewControl (CNTL_RadiosGroup, window);
  157.     EmbedControl (self->mRadiosGroupHandle, self->mRadiosBoxHandle);
  158.     self->mRadioButtonHandle = GetNewControl (CNTL_RadioButton, window);
  159.     EmbedControl (self->mRadioButtonHandle, self->mRadiosGroupHandle);
  160.     SetWindowItemFont (self->mRadioButtonHandle, wftb, 10);
  161.     SetBevelButtonGraphicAlignment (self->mRadioButtonHandle, kControlBevelButtonAlignCenter, 0, 0);
  162.     self->mRadioButton2Handle = GetNewControl (CNTL_RadioButton2, window);
  163.     EmbedControl (self->mRadioButton2Handle, self->mRadiosGroupHandle);
  164.     SetWindowItemFont (self->mRadioButton2Handle, wftb, 11);
  165.     SetBevelButtonGraphicAlignment (self->mRadioButton2Handle, kControlBevelButtonAlignCenter, 0, 0);
  166.  
  167.     self->mInvisibleHandle = GetNewControl (CNTL_Invisible, window);
  168.     SetWindowItemFont (self->mInvisibleHandle, wftb, 12);
  169.  
  170.     self->mCapInvisibleLabel = GetNewControl (CNTL_CapInvisible, window);
  171.     SetWindowItemFont (self->mCapInvisibleLabel, wftb, 13);
  172.     SetControlFromTEXT (self->mCapInvisibleLabel, TEXT_CapInvisible);
  173.  
  174.     AdvanceKeyboardFocus (window);
  175.  
  176.     ShowWindow (window);
  177. }
  178.  
  179. /*----------*/
  180. void    Buttons_Close (
  181.     Buttons*        self)
  182. {
  183.     RemoveResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  184.  
  185.     ((GadgetsDoc*)self->super.mDoc)->mButtonsPtr = nil;
  186.     SetInfo (nil);
  187.     HideWindow (self->super.mWindow);
  188.     DisposeWindow (self->super.mWindow);
  189.  
  190.     DeleteButtons (self);
  191. }
  192.  
  193. /*----------*/
  194. void    Buttons_Track (
  195.     Buttons*        self,
  196.     ControlHandle    whichControl,
  197.     short            whichPart,
  198.     Point            where)
  199. {
  200.     Rect            bounds;
  201.     short            newValue;
  202.  
  203.     if (whichControl == self->mStandardHandle) {
  204.         if (TrackClick (self->mStandardHandle, where)) {
  205.         }
  206.     }
  207.     if (whichControl == self->mRightIconHandle) {
  208.         if (TrackClick (self->mRightIconHandle, where)) {
  209.         }
  210.     }
  211.     if (whichControl == self->mBevelHandle) {
  212.         if (TrackClick (self->mBevelHandle, where)) {
  213.         }
  214.     }
  215.     if (whichControl == self->mXxHandle) {
  216.         if (TrackCheckbox (self->mXxHandle, where)) {
  217.             SetTriangle (self->mData, GetControlValue (self->mXxHandle) != 0);
  218.         }
  219.     }
  220.     if (whichControl == self->mLightHandle) {
  221.         if (TrackClick (self->mLightHandle, where)) {
  222.         }
  223.     }
  224.     if (whichControl == self->mLeftRightHandle) {
  225.         if (TrackCheckbox (self->mLeftRightHandle, where)) {
  226.             SetLeftRight (self->mData, GetControlValue (self->mLeftRightHandle) != 0);
  227.         }
  228.     }
  229.     if (whichControl == self->mRadiosGroupHandle) {
  230.         if (TrackClick (whichControl, where) != 0) {
  231.             SetRadios (self->mData, GetControlValue (self->mRadiosGroupHandle));
  232.         }
  233.     }
  234.     if (whichControl == self->mInvisibleHandle) {
  235.         if (TrackClick (self->mInvisibleHandle, where)) {
  236.         }
  237.     }
  238. }
  239.  
  240. //----------
  241. void    Buttons_DataChanged (
  242.     Buttons*        self,
  243.     long            inDataID)
  244. {
  245.     if (inDataID == idTriangle) {
  246.         SetControlValue (self->mXxHandle, GetTriangle (self->mData));
  247.     }
  248.     if (inDataID == idLeftRight) {
  249.         SetControlValue (self->mLeftRightHandle, GetLeftRight (self->mData));
  250.     }
  251.     if (inDataID == idRadios) {
  252.         SetControlValue (self->mRadiosGroupHandle, GetRadios (self->mData));
  253.     }
  254. }
  255.  
  256. /*----------*/
  257. void    Buttons_MouseIn (
  258.     Buttons*        self,
  259.     Point            where,
  260.     short            modifiers)
  261. {
  262.     Rect        bounds;
  263.  
  264. }
  265.  
  266. //----------
  267. void    Buttons_ExitCurField (
  268.     Buttons*        self)
  269. {
  270.     ControlHandle    focus;
  271.  
  272.     GetKeyboardFocus (self->super.mWindow, &focus);
  273.  
  274.     if (focus == nil) {
  275.         // nothing to exit
  276.  
  277.     }
  278. }
  279.  
  280. /*----------*/
  281. void    Buttons_TypeIn (
  282.     Buttons*        self,
  283.     char            ch)
  284. {
  285.     ControlHandle    focus;
  286.     short            keyCode;
  287.  
  288.     GetKeyboardFocus (self->super.mWindow, &focus);
  289.  
  290.     if ((ch == charEnter)
  291.     ||  (ch == charReturn)) {
  292.         Buttons_ExitCurField (self);    // Dispatch
  293.         SimulateClick (self->mStandardHandle);
  294.     } else if (ch == charEsc) {
  295.     } else if (ch == charTab) {
  296.         AMWindow_DoTab ((AMWindow*) self, (curEvent.modifiers & optionKey) != 0);
  297.     } else if (focus != nil) {
  298.         keyCode = curEvent.message & keyCodeMask;
  299.         HandleControlKey (focus, keyCode, ch, curEvent.modifiers);
  300.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  301.     } else {
  302.         SysBeep (1);
  303.     }
  304. }
  305.  
  306. /*----------*/
  307. void    Buttons_Resize (
  308.     Buttons*        self)
  309. {
  310.     /* application-specific code to resize items in window */
  311. }
  312.  
  313. /*----------*/
  314. void    Buttons_Scroll (
  315.     Buttons*        self,
  316.     short            newValue,
  317.     short            oldValue)
  318. {
  319.     /* application-specific code to scroll window */
  320.     if (gWhichScroll == self->super.vScroll) {
  321.     } else {    // horizontal
  322.     }
  323. }
  324.  
  325. //----------
  326. void    DoUndo (
  327.     Buttons*        self)
  328. {
  329. } // DoUndo
  330.  
  331. //----------
  332. void    DoCut (
  333.     Buttons*        self)
  334. {
  335.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  336.  
  337.     if (curTE != nil) {
  338.         TECut (curTE);
  339.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  340.         scrapDirty = true;
  341.     }
  342. } // DoCut
  343.  
  344. //----------
  345. void    DoCopy (
  346.     Buttons*        self)
  347. {
  348.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  349.  
  350.     if (curTE != nil) {
  351.         TECopy (curTE);
  352.         scrapDirty = true;
  353.     }
  354. } // DoCopy
  355.  
  356. //----------
  357. void    DoPaste (
  358.     Buttons*        self)
  359. {
  360.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  361.  
  362.     if (curTE != nil) {
  363.         TEPaste (curTE);
  364.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  365.     }
  366. } // DoPaste
  367.  
  368. //----------
  369. void    DoClear (
  370.     Buttons*        self)
  371. {
  372.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  373.  
  374.     if (curTE != nil) {
  375.         TEDelete (curTE);
  376.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  377.     }
  378. } // DoClear
  379.  
  380. //----------
  381. void    DoSelectAll (
  382.     Buttons*        self)
  383. {
  384.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  385.  
  386.     if (curTE != nil) {
  387.         TESetSelect (0, 32767, curTE);
  388.     }
  389. } // DoSelectAll
  390.  
  391. //----------
  392. void    DoShowClipboard (
  393.     Buttons*        self)
  394. {
  395. } // DoShowClipboard
  396.  
  397. //----------
  398. Boolean        Buttons_DoCommand (
  399.     Buttons*        self,
  400.     long            inCommand)
  401. {
  402.     Boolean        result = true;
  403.  
  404.     switch (inCommand) {
  405.         case cmdUndo:
  406.                 DoUndo (self);
  407.             break;
  408.         case cmdCut:
  409.                 DoCut (self);
  410.             break;
  411.         case cmdCopy:
  412.                 DoCopy (self);
  413.             break;
  414.         case cmdPaste:
  415.                 DoPaste (self);
  416.             break;
  417.         case cmdClear:
  418.                 DoClear (self);
  419.             break;
  420.         case cmdSelectAll:
  421.                 DoSelectAll (self);
  422.             break;
  423.         case cmdShowClipboard:
  424.                 DoShowClipboard (self);
  425.             break;
  426.  
  427.         default:
  428.                 result = false;
  429.     } // switch
  430.  
  431.     return result;
  432. }
  433.